package com.github.tvbox.osc.util;

import android.util.Base64;

import java.security.MessageDigest;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/*
 * @author: Dozen
 * @description:AES加密和解密
 * @time: 2022/06/17
* */
public class AESUtil {

    public static String keyMe = "dozen729";
    public static String shareSplit = ":::";

    public static String encrypt(String key, String message) {
        try {
            // Use md5 value as the real key
            byte[] b = key.getBytes();
            SecretKeySpec skey = new SecretKeySpec(b,"AES");
            Cipher ecipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            // CBC requires an initialization vector
            ecipher.init(Cipher.ENCRYPT_MODE, skey);
            byte[] plaintext = message.getBytes();
            byte[] result = ecipher.doFinal(plaintext, 0, plaintext.length);
            return Base64.encodeToString(result, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decryptWithAES(String key, String message) {
        try {
            // Use md5 value as the real key
            byte[] b = key.getBytes();
            SecretKeySpec skey = new SecretKeySpec(b, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE, skey);
            byte[] messageData = Base64.decode(message, Base64.DEFAULT);
            byte[] decrypted = cipher.doFinal(messageData);
            return new String(decrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encrypt2(String key, String message) {
        try {
            // Use md5 value as the real key
            byte[] b = key.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] keyData = md.digest(b);

            SecretKeySpec skey = new SecretKeySpec(keyData,"AES");
            // Create an 8-byte initialization vector
            byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
                    0x0e, 0x0f };
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

            Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            // CBC requires an initialization vector
            ecipher.init(Cipher.ENCRYPT_MODE, skey, paramSpec);

            byte[] plaintext = message.getBytes();
            byte[] result = ecipher.doFinal(plaintext, 0, plaintext.length);

            return Base64.encodeToString(result, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decryptWithAES2(String key, String message) {
        try {
            // Use md5 value as the real key
            byte[] b = key.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] keyData = md.digest(b);

            SecretKeySpec skey = new SecretKeySpec(keyData, "AES");
            // Create an 8-byte initialization vector
            byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
                    0x0e, 0x0f };
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

            Cipher dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            // CBC requires an initialization vector
            dcipher.init(Cipher.DECRYPT_MODE, skey, paramSpec);

            byte[] messageData = Base64.decode(message, Base64.DEFAULT);
            byte[] result = dcipher.doFinal(messageData, 0, messageData.length);

            return new String(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

